import modelExtend from "dva-model-extend";
import { parse } from "qs";
import { pageModel } from "./common";
import {
  organ,
  group,
  type,
  documentList,
  newFile,
  upChange,
  newGzcFile1,
  newGzcFileSave1,
  changeGzcFile1,
  changeGzcFileSave1,
  getGZCmatter1,
  getPerson1
} from "services/newfile";
import { infoDetais, userLis } from "services/search";
import { Modal } from "antd";

export default modelExtend(pageModel, {
  namespace: "newfile",
  state: {
    department1: "",
    department2: "",
    depTypes: "",
    infoList: "",
    infoData: "",
    userList: "",
    matter:"",
    contractor:"", //承办人  公证员
    archiving:"", //建档人  档案员
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        const id = location.search;
        if (location.search != "") {
          const ids = location.search.split("?")[1];
          dispatch({
            type: "info",
            payload: { id: ids }
          });
        } else {
          dispatch({
            type: "getNew"
          });
        }
        // if (location.pathname === '/file/newfile'){
        //   dispatch({
        //     type:"getGZCmatter"
        //   })
        // }
        if (location.pathname === "/file/newfile" || location.pathname === "/borrowManager/borrow") {
          dispatch({
            type: "groups",
            payload: location.query
          });
          dispatch({
            type: "depType",
            payload: location.query
          });
          dispatch({
            type: "useLists"
          });
        }
        if(location.pathname === "/file/newfile"){
          dispatch({
            type: "getContractor",
          });
          dispatch({
            type: "getArchiving",
          });
        }
      });
    }
  },
  effects: {
    *getContractor({payload},{call,put}){
      const data = yield call(getPerson1,{roleName:"公证员"})
      if(data){
        yield put({
          type:"getContractorSuccess",
          payload:{
            contractor:data.userList
          }
        })
      }
    },
    *getArchiving({payload},{call,put}){
      const data = yield call(getPerson1,{roleName:"档案员"})
      if(data){
        yield put({
          type:"getArchivingSuccess",
          payload:{
            archiving:data.userList
          }
        })
      }
    },
    *create1({ payload }, { call, put }) {
      const data = yield call(newGzcFile1, payload);
      Modal.confirm({
        title: "提示",
        content: "操作成功",
        okText: "确认",
        cancelText: "取消",
        onOk: function() {
          //跳转到列表
          window.location = "/file/fileComponent";
        },
        onCancel:function(){
          location.reload()
        }
      });
    },
    *createApply({ payload }, { call, put }) {
      const data = yield call(newGzcFileSave1, payload);
      Modal.confirm({
        title: "提示",
        content: "操作成功",
        okText: "确认",
        cancelText: "取消",
        onOk: function() {
          //跳转到列表
          window.location = "/file/fileComponent";
        },
        onCancel:function(){
          location.reload()
        }
      });
    },
    *update({ payload }, { call, put }) {
      const data = yield call(changeGzcFile1, payload);
      Modal.confirm({
        title: "提示",
        content: "操作成功",
        okText: "确认",
        cancelText: "取消",
        onOk: function() {
          //跳转到列表
          window.location = "/file/fileComponent";
        },
        onCancel:function(){
          location.reload()
        }
      });
    },
    *updateApply({ payload }, { call, put }) {
      const data = yield call(changeGzcFileSave1, payload);
      Modal.confirm({
        title: "提示",
        content: "操作成功",
        okText: "确认",
        cancelText: "取消",
        onOk: function() {
          //跳转到列表
          window.location = "/file/fileComponent";
        },
        onCancel:function(){
          location.reload()
        }
      });
    },
    *create({ payload }, { call, put }) {
      const data = yield call(newFile, payload);
      Modal.confirm({
        title: "提示",
        content: "操作成功",
        okText: "确认",
        cancelText: "取消",
        onOk: function() {
          //跳转到列表
          window.location = "/file/fileComponent";
        },
        onCancel:function(){
          location.reload()
        }
      });
    },
    *upDocument({ payload }, { call, put }) {
      const data = yield call(upChange, payload);
      Modal.confirm({
        title: "提示",
        content: "操作成功",
        okText: "确认",
        cancelText: "取消",
        onOk: function() {
          //跳转到列表
          window.location = "/file/fileComponent";
        },
        onCancel:function(){
          location.reload()
        }
      });
    },
    *info({ payload }, { call, put }) {
      const data = yield call(infoDetais, payload);
      if (data) {
        yield put({
          type: "querySuccessA",
          payload: {
            infoData: data
          }
        });
        // yield put({
        //   type: "dep",
        //   payload: { parentId: data.document.organizationId }
        // });
      }
    },
    *groups({ payload }, { call, put }) {
      const data = yield call(organ, payload);
      if (data.success) {
        yield put({
          type: "querySuccess",
          payload: {
            department1: data.list
          }
        });
      }
    },
    *dep({ payload }, { call, put }) {
      const data = yield call(group, payload);
      if (data.success) {
        yield put({
          type: "querySuccess1",
          payload: {
            department2: data.list
          }
        });
      }
    },
    *depType({ payload }, { call, put }) {
      const data = yield call(type, payload);
      if (data.success) {
        yield put({
          type: "querySuccess2",
          payload: {
            depTypes: data.list
          }
        });
      }
    },
    *menuList({ payload }, { call, put }) {
      const data = yield call(documentList, payload);
      if (data.success) {
        yield put({
          type: "querySuccess3",
          payload: {
            infoList: data.template
          }
        });
      }
    },
    *useLists({ payload }, { call, put }) {
      const data = yield call(userLis, payload);
      if (data.success) {
        yield put({
          type: "queryUserSuccess",
          payload: {
            userList: data.userList
          }
        });
      }
    },
    *getGZCmatter({ payload },{ call, put }){
      const data = yield call(getGZCmatter1,{})
      if(data){
        yield put({
          type:"getGZCmatterSuccess",
          payload:{
            matter:data.list
          }
        })
      }
    }
  },
  reducers: {
    getGZCmatterSuccess(state, action){
      const { matter } = action.payload
      return { ...state,matter }
    },
    queryUserSuccess(state, action) {
      const { userList } = action.payload;
      return { ...state, userList };
    },
    getNew(state) {
      return { ...state, infoData: "" };
    },
    querySuccess(state, action) {
      const { department1 } = action.payload;
      return {
        ...state,
        department1
      };
    },
    querySuccess1(state, action) {
      const { department2 } = action.payload;
      return {
        ...state,
        department2
      };
    },
    querySuccess2(state, action) {
      const { depTypes } = action.payload;
      return {
        ...state,
        depTypes
      };
    },
    querySuccess3(state, action) {
      const { infoList } = action.payload;
      return {
        ...state,
        infoList
      };
    },
    querySuccessA(state, action) {
      const { infoData } = action.payload;
      return {
        ...state,
        infoData
      };
    },
    getContractorSuccess(state, action){
      const { contractor } = action.payload;
      return{
        ...state,
        contractor,
      }
    },
    getArchivingSuccess(state, action){
      const { archiving } = action.payload;
      return{
        ...state,
        archiving
      }
    }
  }
});
